#include "Rectangle.h"

Rectangle::Rectangle(const float x, const float y, const float width, const float height)
{
	this->x = x;
	this->y = y;
	this->width = width;
	this->height = height;
}

Rectangle::Rectangle(const Vector2D &topLeft, const Vector2D &btmRight)
{
	if(topLeft.x <= btmRight.x)
	{
		x = topLeft.x;
		width = btmRight.x - topLeft.x;
	}
	else // >
	{
		x = btmRight.x;
		width = topLeft.x - btmRight.x;
	}

	if(topLeft.y <= btmRight.y)
	{
		y = topLeft.y;
		height = btmRight.y - topLeft.y;
	}
	else // >
	{
		y = btmRight.y;
		height = topLeft.y - btmRight.y;
	}
}

bool Rectangle::Contains(const float x, const float y) const
{
	if (x >= this->x && x <= this->x + this->width &&
		y >= this->y && y <= this->y + this->height)
	{
		return true;
	}

	return false;
}

bool Rectangle::Contains(const Vector2D &point) const
{
	return Contains(point.x, point.y);
}


void Rectangle::SetPosition(const float x, const float y)
{
	this->x = x;
	this->y = y;
}

void Rectangle::SetPosition(const Vector2D &vect)
{
	SetPosition(vect.x, vect.y);
}

void Rectangle::MoveBy(const float x, const float y)
{
	this->x += x;
	this->y += y;
}

void Rectangle::MoveBy(const Vector2D &vect)
{
	MoveBy(vect.x, vect.y);
}


bool Rectangle::CollidesWith(const Rectangle &rect) const
{
    float dx = fabs(rect.x - x) + rect.width;
    float dy = fabs(rect.y - y) + rect.height;

    return (dx < width + rect.width && dy < height + rect.height);
}